package com.zx.disneyholidays.page.coupon.list;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zx.disneyholidays.R;
import com.zx.disneyholidays.bean.TicketCardBean;
import com.zx.disneyholidays.page.coupon.CouponDetailActivity;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ItemViewHolder> {

    Context context;
    LayoutInflater layoutInflater;
    List<TicketCardBean.CouponBean> list = new ArrayList<>();

    public ListAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<TicketCardBean.CouponBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(layoutInflater.inflate(R.layout.item_coupon_list_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.bindView(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView couponType;
        TextView couponName;
        TextView tvDiscount;
        TextView tvTimes;
        TextView tvEndDate;
        TextView tvConfirm;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            couponType = itemView.findViewById(R.id.tv_coupon_type);
            couponName = itemView.findViewById(R.id.tv_coupon_name);
            tvDiscount = itemView.findViewById(R.id.tv_discount);
            tvTimes = itemView.findViewById(R.id.tv_times);
            tvEndDate = itemView.findViewById(R.id.tv_end_date);
            tvConfirm = itemView.findViewById(R.id.tv_confirm);

        }

        @SuppressLint("SetTextI18n")
        public void bindView(TicketCardBean.CouponBean couponBean) {
            couponType.setText(couponBean.getType());
            couponName.setText(couponBean.getName());
            tvDiscount.setText(couponBean.getDiscount());
            tvTimes.setText("本周剩余" + couponBean.getWeek_number() + "次");
            tvEndDate.setText("使用有效期至：\n" + couponBean.getEnd_date());

            tvConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(tvConfirm.getContext(), CouponDetailActivity.class);
                    intent.putExtra("couponId", couponBean.getId());
                    intent.putExtra("ticketId", CouponListActivity.ticketId);
                    tvConfirm.getContext().startActivity(intent);
                }
            });
        }
    }
}
